﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Reflection;
using System.IO;
using Microsoft.Build.Logging;

namespace Build.UnitTests
{
	[TestClass]
	public class BuildLoggerTest
	{
		private String SamplesPath;

		[TestInitialize]
		public void Initialize()
		{
			Assembly asm = Assembly.GetExecutingAssembly();
			SamplesPath = asm.Location;
			SamplesPath = SamplesPath.Remove(SamplesPath.IndexOf(asm.GetName().Name));
			SamplesPath = Path.Combine(SamplesPath, "Build\\Samples");
		}

		private BuildAppInstance SetTestParametersAndExecute(String iTestName)
		{
			String EbdPath = Path.Combine(SamplesPath, String.Format("{0}.ebd", iTestName));
			BuildAppInstance app = new BuildAppInstance(EbdPath, true);
			app.Run();
			return app;
		}

		[TestMethod]
		public void TestWithAllParams()
		{
			var app = SetTestParametersAndExecute("TestWithAllParams");
			Assert.AreEqual("logfile=Y:\\DefaultCollection\\DistributionProject\\Development\\Distribution\\BuildLog.log;Append;", app.Logger.Parameters);
		}

		[TestMethod]
		[ExpectedException(typeof(BuildException))]
		public void TestWithLoggerModeInvalid()
		{
			var app = SetTestParametersAndExecute("TestWithLoggerModeInvalid");
			Assert.IsNull(app.Logger);
		}

		[TestMethod]
		public void TestWithLoggerOff()
		{
			var app = SetTestParametersAndExecute("TestWithLoggerOff");
			Assert.IsNull(app.Logger);
		}

		[TestMethod]
		public void TestWithLoggerOverwrite()
		{
			var app = SetTestParametersAndExecute("TestWithLoggerOverwrite");
			Assert.AreEqual("logfile=Y:\\DefaultCollection\\DistributionProject\\Development\\Distribution\\BuildLog.log;Append;", app.Logger.Parameters);
		}

		[TestMethod]
		[ExpectedException(typeof(BuildException))]
		public void TestWithLoggerPathEmpty()
		{
			var app = SetTestParametersAndExecute("TestWithLoggerPathEmpty");
			Assert.IsNull(app.Logger);
		}

		[TestMethod]
		[ExpectedException(typeof(BuildException))]
		public void TestWithLoggerModeInvalidAndEmptyPath()
		{
			var app = SetTestParametersAndExecute("TestWithLoggerModeInvalidAndEmptyPath");
			Assert.IsNull(app.Logger);
		}
	}
}
